﻿using BusinessLayer;
using EntitiesLayer;
using QuiddichWPF.Config;
using QuidditchWpf.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuiddichWPF
{
    /// <summary>
    /// Logique d'interaction pour MatchWindow.xaml
    /// </summary>
    public partial class MatchWindow : Window
    {
        private GestionEntitiesViewModel gestionVm;

        public MatchWindow()
        {
            InitializeComponent();

            gestionVm = new GestionEntitiesViewModel();
        }

        /// <summary>
        /// Déclenché lors du clic sur la fenêtre principale afin de pouvoir la déplacer
        /// </summary>
        /// <param name="sender"> Sender object </param>
        /// <param name="e"> Arguments </param>
        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Configuration.ConfigurationUtility.RegisterWindowConfig(Window.GetWindow(this));
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            Configuration.ConfigurationUtility.Configure(Window.GetWindow(this));
        }

        private void cupsCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //listMatches.ItemsSource = CoupeManager.Manager.Matchs.Where(m => m.ID == (cupsCombo.SelectedItem as Coupe).ID);
            listMatches.ItemsSource = gestionVm.ViewMatchModel.Where(m => m.CoupeID == (cupsCombo.SelectedItem as Coupe).ID || m.ID == 0);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //cupsCombo.SelectedIndex = 0;
        }

        private void Modify_MouseClick(object sender, RoutedEventArgs e)
        {
            ContentPresenter contentPresenter = Helper.FindVisualChild<ContentPresenter>(content);
            DataTemplate template = contentPresenter.ContentTemplate;

            TextBox year = (TextBox)template.FindName("year", contentPresenter);
            BindingExpression be = year.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();
        }

        private void stade_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            if (cb.SelectedValue != null) 
            {
                String nomStade = cb.SelectedValue.ToString().Split(':')[0];

                Match match = (Match)listMatches.SelectedItem;
                match.Stade = CoupeManager.Manager.Stadiums.First(st => st.Nom.Equals(nomStade));
            }
        }

        private void equiVis_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            if (cb.SelectedValue != null)
            {
                String nomEquipeVis = cb.SelectedValue.ToString();

                Match match = (Match)listMatches.SelectedItem;
                match.EquipeVisiteuse = CoupeManager.Manager.Teams.First(eq => eq.Nom.Equals(nomEquipeVis));
            }
        }

        private void equiDom_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            if (cb.SelectedValue != null) 
            {
                String nomEquipeDom = cb.SelectedValue.ToString();

                Match match = (Match)listMatches.SelectedItem;
                match.EquipeDomicile = CoupeManager.Manager.Teams.First(eq => eq.Nom.Equals(nomEquipeDom));
             }
        }
    }
}
